<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/15
 * Time: 14:49
 */

namespace App\LogicService;

use App\LogicService\Contracts\UserFriendsLogicContract;
use App\Models\UserFriends;


class UserFriendsLogic extends BaseLogic implements UserFriendsLogicContract
{


    /**
     * @param $data
     * @return mixed
     */
    public function store($data)
    {
        $data['type'] = 1;
        $id = UserFriends::create($data);
        return $id;

    }

    /**
     * @param $user_account
     * @param $f_user_account
     * @return bool
     */

    public function delete($user_account, $f_user_account)
    {
        $result = false;
        $userFriends = UserFriends::where('user_account', $user_account)
            ->where('f_user_account', $f_user_account)
            ->first();
        if ($userFriends) {
            $result = $userFriends->delete();
        }
        return $result;
    }

    /**
     * @param $user_account
     * @param $f_user_account
     * @param $memo_name
     * @return mixed
     */
    public function update($user_account, $f_user_account, $memo_name)
    {
        $userFriends = UserFriends::where('user_account', $user_account)
            ->where('f_user_account', $f_user_account)
            ->first();
        $userFriends->memo_name = $memo_name;
        $result = $userFriends->save();
        return $result;
    }

    /**
     * @param $user_account
     * @param $user_type
     * @param int $type
     * @return mixed
     */
    public function getList($user_account, $user_type, $type = 1)
    {

        if($user_type == 0){
            $result = UserFriends::where('user_account', $user_account)
                ->where('type', $type)
                ->get();
            return $result;
        }

        if ($user_type == 1) {
            $symbol = '=';
        } else {
            $symbol = '<>';
        }
        $result = UserFriends::where('st_user_friends.user_account', $user_account)
            ->where('st_user_friends.type', $type)
            ->where('tn_user_base.type', $symbol, 1)
            ->join('tn_user_base', 'st_user_friends.f_user_account', '=', 'tn_user_base.account')
            ->select('st_user_friends.*')
            ->get();


        return $result;
    }

    public function show($user_account, $f_user_account)
    {

        $userFriends = UserFriends::where('user_account', $user_account)
            ->where('f_user_account', $f_user_account)
            ->first();

        return $userFriends;
    }
}